package artibella.loaders 
{
	import artibella.IDisposable;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	/**
	 * ...
	 * @author pbordachar
	 *
	 */
	
	public class LoaderURL extends EventDispatcher implements IDisposable
	{
		
		private var _chargeur:URLLoader; 
		private var _formatDonnees:String;
		private var _request:URLRequest;
		private var _function:Function;
	
		private var _resultat:*;
		
		
		public function LoaderURL( fichier:String, format:String = URLLoaderDataFormat.TEXT, variables:URLVariables = null, methode:String = URLRequestMethod.POST, fonction:Function = null  )
		{
			var stamp:String = ( methode == URLRequestMethod.POST ) ? _cache() : "";
			
			_request = new URLRequest( fichier + stamp );
			_request.data = variables;
			_request.method = methode;
			
			_formatDonnees = format;
			
			_function = fonction;
		}
		
		public function load():void
		{
			_chargeur = new URLLoader();
			_chargeur.dataFormat = _formatDonnees;
			
			_addLoaderListeners();
			
			_chargeur.load( _request );
		}
		
		///////  E C O U T E U R S
		
		private function _addLoaderListeners( ):void
		{
			_chargeur.addEventListener( HTTPStatusEvent.HTTP_STATUS, _gestionHTTPStatus );
			_chargeur.addEventListener( IOErrorEvent.IO_ERROR, _gestionErreur );
			_chargeur.addEventListener( SecurityErrorEvent.SECURITY_ERROR, _gestionErreur );
			_chargeur.addEventListener( Event.COMPLETE, _chargementTermine );
		}
		
		private function _removeLoaderListeners( ):void
		{
			if( _chargeur.hasEventListener( Event.COMPLETE ) ){
				_chargeur.removeEventListener( HTTPStatusEvent.HTTP_STATUS, _gestionHTTPStatus );
				_chargeur.removeEventListener( IOErrorEvent.IO_ERROR, _gestionErreur );
				_chargeur.removeEventListener( SecurityErrorEvent.SECURITY_ERROR, _gestionErreur );
				_chargeur.removeEventListener( Event.COMPLETE, _chargementTermine );
			}
		}
		
		///////  C H A R G E M E N T 
		
		private function _gestionHTTPStatus( e:HTTPStatusEvent ):void 
		{ 
			//trace( e );
		}
		
		private function _gestionErreur( e:Event ):void
		{ 
			trace( e );
			
			_removeLoaderListeners();
		}
		
		private function _chargementTermine( e:Event ):void
		{
			_removeLoaderListeners();
			
			_resultat = e.target.data;
			
			dispatchEvent( e );
			
			if( _function != null ) _function();
		}
		
		/////// U T I L I T A I R E S
		
		public function get resultat():*
		{
			return _resultat;
		}
		
		private function _cache():String
		{
			var time:Date = new Date();
			var timeStamp:String = "?" + time.getTime();
			return timeStamp;
		}
		
		///////  D E S T R U C T I O N
		
		public function dispose():void
		{
			_removeLoaderListeners();

			_request = null;
			_chargeur = null;
			_function = null;
			
			_resultat = null;
		}
	}
}